package mylogs

import (
	"fmt"
	"strings"
	"time"
)

// 往终端写日志相关内容
type LogLevel uint16

const (
	DEBUG LogLevel = iota
	TRACE
	INFO
	WARING
	ERROR
	FATAL
)

func parseLogLevel(s string) (LogLevel, error) {
	s = strings.ToLower(s)
	switch s {
	case "debug":
		return DEBUG, nil
	case "trace":
		return TRACE, nil
	case "info":
		return INFO, nil
	case "warning":
		return WARING, nil
	case "error":
		return ERROR, nil
	case "fatal":
		return FATAL, nil
	default:
		return DEBUG, nil
	}
}

// Logger 日志结构体
type Logger struct {
	level LogLevel
}

// NewLog 构造函数
func NewLog(levelStr string) Logger {
	level, err := parseLogLevel(levelStr)
	if err != nil {
		fmt.Printf("日志系统错误:%v\n", err)
	}
	return Logger{
		level: level,
	}
}

func (l Logger) enable(logLevel LogLevel) bool {

	return l.level >= logLevel
}

func (l Logger) Debug(msg string) {
	if l.enable(DEBUG) {

		fmt.Println(msg)
	}
}

func (l Logger) Info(msg string) {
	if l.enable(INFO){
		now := time.Now()
		fmt.Printf("[%s] %s\n", now.Format("2006-01-02 15:04:05"), msg)
	}

}

func (l Logger) Warning(msg string) {
	if l.enable(WARING){
		fmt.Println(msg)
	}
}

func (l Logger) Error(msg string) {
	if l.enable(ERROR){
		fmt.Println(msg)
	}
}

func (l Logger) Fatal(msg string) {
	if l.enable(FATAL){
		fmt.Println(msg)
	}
}
